package com.hangsu.train.components.service.impl;

import com.hangsu.train.components.dao.WandedDao;
import com.hangsu.train.components.entity.Wanded;
import com.hangsu.train.components.params.IdParam;
import com.hangsu.train.components.params.WandedSearchParam;
import com.hangsu.train.components.service.WandedService;
import com.hangsu.train.core.keygen.KeyGenerate;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lyy
 * @Date 2024/11/17
 */
@Service
public class WandedServiceImpl implements WandedService {

    @Resource
    private WandedDao wandedDao;

    @Resource
    private KeyGenerate keyGenerate;

    @Override
    public Page<Wanded> list(WandedSearchParam searchParam) {
        PageRequest request=PageRequest.of(searchParam.getPage(), searchParam.getPageSize());
        Wanded query=new Wanded();
        if(StringUtils.isNotEmpty(searchParam.getName())){
            query.setName(searchParam.getName());
        }
        List<Wanded> list =  wandedDao.list(query,request.getOffset(),request.getPageSize());
        long count = wandedDao.count(query);
        return new PageImpl<>(list,request,count);
    }

    @Override
    public Wanded detail(IdParam idParam) {
        return wandedDao.selectByPrimaryKey(idParam.getId());
    }

    @Override
    public long save(Wanded wanded) {
        wanded.setId((Long) keyGenerate.generateKey());
        wandedDao.insert(wanded);
        return wanded.getId();
    }

    @Override
    public void update(Wanded wanded) {
        wandedDao.updateBySelective(wanded);
    }

    @Override
    public void del(IdParam idParam) {
        wandedDao.deleteByPrimaryKey(idParam.getId());
    }

    @Override
    public List<Wanded> search(WandedSearchParam searchParam) {
        Wanded query=new Wanded();
        query.setName(searchParam.getName());
        return wandedDao.search(query);
    }
}